/**
 */

package com.bei_en.common.util.amap;

import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.SortedMap;

import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bei_en.common.util.HttpClientUtils;
import com.bei_en.teacher.admin.other.Consts;
import com.google.gson.Gson;

/**
 * @author Guocg
 *
 */
public class AmapStoreUtil {

	protected static Logger log = LoggerFactory.getLogger(AmapStoreUtil.class);

	private static final String key = "bbdb78f398b6628612b4d2013e0b709f";
	private static final String secret = "4a083e07b8906eadc687f6e7f1953200";

	private static final String tableId = "56de45f1305a2a3288f492e5";

	private static Gson gson = new Gson();

	private static String sendRequest(String url, String data) {

		String re = HttpClientUtils.post(url, data, Consts.TIMEOUT);
		if (re.equals("失败")) {
			log.error("高德后台返回--失败");
		}
		if (re.equals("超时")) {
			log.error("高德后台返回--超时");
		}
		return re;
	}

	private static String handle(SortedMap map)
			throws UnsupportedEncodingException {

		String re = "", re1 = "";
		map.put("key", key);
		for (Iterator it = map.keySet().iterator(); it.hasNext();) {
			String i = (String) it.next();
			re += i + "=" + map.get(i) + "&";
		}
		re1 = re.substring(0, re.length() - 1) + secret;
		log.info(re1);
		String sig = DigestUtils.md5Hex(re1);
		log.info(sig);
		return re + "sig=" + sig;
	}

	public static String createTable(SortedMap map)
			throws UnsupportedEncodingException {

		if (map.isEmpty())
			return "";
		String data = handle(map);
		log.info(data);
		String re = sendRequest(Consts.AMAP_CREATE_TABLE, data);
		RespObj1 jsonResult = gson.fromJson(re, RespObj1.class);
		if (jsonResult.getStatus() != 1) {
			log.error(jsonResult.getInfo());
			return "";
		}
		return jsonResult.getTableid();
	}

	public static String createData(SortedMap map)
			throws UnsupportedEncodingException {

		if (map.isEmpty())
			return "";
		map.put("tableid", tableId);
		String data = handle(map);
		log.info(data);
		String re = sendRequest(Consts.AMAP_CREATE_DATA, data);
		RespObj2 jsonResult = gson.fromJson(re, RespObj2.class);
		if (jsonResult.getStatus() != 1) {
			log.error(jsonResult.getInfo());
			return "";
		}
		return jsonResult.get_id();
	}

	public static String createDataBatch(SortedMap map)
			throws UnsupportedEncodingException {

		if (map.isEmpty())
			return "";
		// --
		// String re = sendRequest(Consts.AMAP_CREATE_DATA_BATCH, data);
		return "";
	}

	public static String queryDataBatch(SortedMap map)
			throws UnsupportedEncodingException {

		if (map.isEmpty())
			return "";
		// --
		// String re = sendRequest(Consts.AMAP_QUERY_BATCH, data);
		return "";
	}

	public static String updateData(SortedMap map)
			throws UnsupportedEncodingException {

		if (map.isEmpty())
			return "";
		map.put("tableid", tableId);
		String data = handle(map);
		log.info(data);
		String re = sendRequest(Consts.AMAP_UPDATE_DATA, data);
		RespObj2 jsonResult = gson.fromJson(re, RespObj2.class);
		if (jsonResult.getStatus() != 1) {
			log.error(jsonResult.getInfo());
			return "";
		}
		return "ok";
	}

	public static String deleteData(SortedMap map)
			throws UnsupportedEncodingException {

		if (map.isEmpty())
			return "";
		map.put("tableid", tableId);
		String data = handle(map);
		log.info(data);
		String re = sendRequest(Consts.AMAP_DELETE_DATA, data);
		RespObj4 jsonResult = gson.fromJson(re, RespObj4.class);
		if (jsonResult.getStatus() != 1) {
			log.error(jsonResult.getInfo());
			return "";
		}
		return jsonResult.getSuccess() + "," + jsonResult.getFail();
	}
}
